package com.totoro.tygeneratedragonblock.executor.computeparse.impl;

import com.totoro.tygeneratedragonblock.constants.ComputeTypeConst;
import com.totoro.tygeneratedragonblock.data.ComputeResult;
import com.totoro.tygeneratedragonblock.executor.computeparse.ComputeParseExecutor;
import com.totoro.tygeneratedragonblock.utils.JexlUtil;
import com.totoro.tygeneratedragonblock.utils.PapiUtil;
import com.totoro.tygeneratedragonblock.utils.StrUtil;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

/**
 * @author Totoro
 * @version 0.1.0
 * @date 2025/8/11 - 0:00
 * @description
 **/
public class DefaultComputeParseExecutor implements ComputeParseExecutor {
    @Override
    public ComputeResult execute(Player player, String computeText) {
        String parsePapiContent = PapiUtil.parseStrPapiVariable(player, computeText);
        // 获取expression:后的内容，这个内容是用来计算概率的，比如0.1
        String finalContent = StrUtil.getPrefixAfter(parsePapiContent, ComputeTypeConst.EXPRESSION);
        // 计算表达式
        Number result = (Number) JexlUtil.evaluateExpressions(finalContent);
        return new ComputeResult(ComputeTypeConst.EXPRESSION, result, null);
    }
}
